<html>
<head><meta charset="utf-8"><title>ConstChunksExact · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/ConstChunksExact.html">ConstChunksExact</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="192177999"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/ConstChunksExact/near/192177999" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DutchGhost <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/ConstChunksExact.html#192177999">(Mar 29 2020 at 14:48)</a>:</h4>
<p>Hi, Im trying to implement <a href="https://github.com/rust-lang/rust/issues/60735" title="https://github.com/rust-lang/rust/issues/60735">https://github.com/rust-lang/rust/issues/60735</a>.<br>
However I had a question.<br>
Since ChunksExact and friends already exist, is it okey to just make a wrapper around them?:</p>
<div class="codehilite"><pre><span></span><span class="k">struct</span> <span class="nc">ConstChunksExact</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">inner</span>: <span class="nc">ChunksExact</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="o">&gt;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Iterator</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">ConstChunksExact</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="p">{</span><span class="n">N</span><span class="p">}</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">Item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="na">&#39;a</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">next</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Option</span><span class="o">&lt;</span><span class="n">Self</span>::<span class="n">Item</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="cm">/* snip */</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>this would reduce duplication in the logic for the iterators, and only do a bit of type fiddling.</p>
<p>The other option is to rewrite all the logic based on the implementation of ChunksExact, but this duplicates alot.</p>



<a name="192178169"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/ConstChunksExact/near/192178169" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/ConstChunksExact.html#192178169">(Mar 29 2020 at 14:52)</a>:</h4>
<p>If we can do that (i.e. it still optimizes nicely) then we should, yes. I suspect it would though, as I've had success with just the regular chunks_exact with a constant argument.</p>



<a name="192180196"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/ConstChunksExact/near/192180196" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DutchGhost <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/ConstChunksExact.html#192180196">(Mar 29 2020 at 15:41)</a>:</h4>
<p>One downside is that the size of the ConstChunksExact has 8 bytes overhead when wrapping. The reason is that the normal ChunksExact keeps a size parameter, which we dont need any more</p>



<a name="192180422"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/ConstChunksExact/near/192180422" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/ConstChunksExact.html#192180422">(Mar 29 2020 at 15:45)</a>:</h4>
<p>I doubt this is a major concern in practice, and it has upsides too: less code is mononorphized for each constant N, improving compile times and code size if multiple chunk sizes are used in one application</p>



<a name="192180426"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/ConstChunksExact/near/192180426" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/ConstChunksExact.html#192180426">(Mar 29 2020 at 15:45)</a>:</h4>
<p>In practice that's almost always going to be optimized away by the compiler.</p>



<a name="192180474"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/ConstChunksExact/near/192180474" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/ConstChunksExact.html#192180474">(Mar 29 2020 at 15:46)</a>:</h4>
<p>If it becomes a problem in the future we can always look into fixing that.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>